package com.hushan.interceptor;

import com.hushan.service.LogService;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 注解方式定义切点
 * Created by hushan on 2017/6/20.
 */
@Aspect
@Component
public class LogAop {

    // 注入logService用于保存日志到数据库
    @Autowired
    private LogService logService;

    // 本地日志记录
    private static final Logger logger = Logger.getLogger(LogAop.class);

    // 定义切点
    @Pointcut("@annotation(com.hushan.annotion.LogMethod)")
    private void pointCut() {
    }

    @Before("pointCut()")
    public void beforeExcute(JoinPoint joinPoint) {

    }

    @After("pointCut()")
    public void afterExcute(JoinPoint joinPoint) {
        StringBuffer sb = new StringBuffer();

        String kind = joinPoint.getKind();
        String name = joinPoint.getSignature().getName();
        Object[] objects = joinPoint.getArgs();
        String typeName = joinPoint.getSignature().getDeclaringTypeName();
        joinPoint.getTarget().getClass().getName();  // log Begining method
        System.out.println("kind=" + kind);
        System.out.println("name=" + name);
        System.out.println("typeName=" + typeName);

        for (Object param : objects) {
            sb.append(param);
        }
        System.out.println(sb.length() > 0 ? sb : "无参");

    }

}
